#ifndef __STUN_REQUEST_H
#define __STUN_REQUEST_H
#include <map>
#include <rtc_base/third_party/sigslot/sigslot.h>
#include "ice/stun.h"
namespace xrtc
{

    class StunRequest;

    class StunRequestManager : public sigslot::has_slots<>
    {

    public:
        StunRequestManager() = default;
        ~StunRequestManager();
        void send(StunRequest *request);
        sigslot::signal3<StunRequest *, const char *, size_t> signal_send_packet;
        bool check_response(StunMessage *msg);
        void remove(StunRequest *request);

    private:
        typedef std::map<std::string, StunRequest *> RequestMap;
        RequestMap _requests;
    };
    class StunRequest
    {
    public:
        StunRequest(StunMessage *msg);
        virtual ~StunRequest();
        const std::string &id() { return _msg->transcation_id(); }
        void construct();
        void send();
        void set_manager(StunRequestManager *manager) { _manager = manager; }
        int type() { return _msg->type(); }
        int elapsed();

    protected:
        virtual void prepare(StunMessage *) {};
        virtual void on_request_response(StunMessage *) {};
        virtual void on_request_error_response(StunMessage *) {};
        friend class StunRequestManager;

    private:
        StunMessage *_msg;
        StunRequestManager *_manager = nullptr;
        int64_t _ts = 0;
    };

} // namespace xrtc;

#endif